package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.model.OriginField;
import com.jiantong.lawEnforcing.service.OriginFieldService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

/**
 * 元数据表
 * Created by mz03 on 2017/9/14.
 */
@Slf4j
@OpenBean
public class OriginFieldApis {
    @RpcConsumer
    private OriginFieldService originFieldService;



    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.originField.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public OriginField findById(Long id){
        Response<OriginField> response = originFieldService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find originField read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     * @param originField
     * @return
     */
    @OpenMethod(
            key = "create.originField",
            paramNames = {"Goods"},
            httpMethods = RequestMethod.POST
    )
    public Long create(OriginField originField){
        Response<Long> response = originFieldService.create(originField);
        if(!response.isSuccess()){
            log.error("failed to create originField read by originField = {}, cause : {}", originField, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     */
    @OpenMethod(
            key = "update.ask.originField",
            paramNames = {"originField"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(OriginField originField){
        if(originField.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = originFieldService.update(originField);
        if(!response.isSuccess()){
            log.error("failed to update originField");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update originField");
        }
        return response.getResult();
    }

}
